using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using PropertyChanged;
using WPFTemplateLib.WpfConverters;
using WPFTemplateLib.WpfConverters.Core;
using WPFTemplateLib.WpfHelpers;

namespace WPFPractice.Dialogs
{
	/// <summary>
	/// 选择枚举项通用窗口（通过设置键值对字符串来显示自定义内容）
	/// </summary>
	[AddINotifyPropertyChangedInterface]
	public partial class SelectEnumCustomTextDialog : Window
	{
		#region 成员&构造

		/// <summary>
		/// 显示自定义文本的转换器
		/// </summary>
		public static FuncValueConverter<Enum, string> ShowCustomTextConverter { get; private set; }

		private SelectEnumCustomTextDialog()
		{
			ShowCustomTextConverter = new(GetCustomText);

			InitializeComponent();
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="titleName">标题</param>
		/// <param name="options">枚举列表</param>
		/// <param name="selectedItem">当前选中的枚举</param>
		/// <param name="keyValuePairStr">用于自定义显示的键值对字符串（以 | 分组，组内以 , 或 : 分隔出 key 和 value），形如："Show,浏览 | Edit,编辑 | Add,添加"</param>
		public SelectEnumCustomTextDialog(string titleName, IEnumerable<Enum> options, Enum selectedItem, string keyValuePairStr) : this()
		{
			TitleName = titleName;
			Options = options;
			SelectedItem = selectedItem;

			_keyValuePairStr = keyValuePairStr;

			Owner = WpfWindowHelper.GetActiveWindow();
		}

		/// <summary>
		/// 键值对字符串（以 | 分组，组内以 , 或 : 分隔出 key 和 value）<para/>
		/// 形如："Show,浏览 | Edit,编辑 | Add,添加" 或 "0:开始 | 1:起步 | 2:加速 | 3:冲锋 | 4:减速 | 5:停止"
		/// </summary>
		private readonly string _keyValuePairStr;

		#endregion

		#region 绑定

		/// <summary>
		/// 标题名称
		/// </summary>
		public string TitleName { get; set; }

		/// <summary>
		/// 候选项（枚举集合）
		/// </summary>
		public IEnumerable<Enum> Options { get; set; }

		/// <summary>
		/// 选择项（枚举）
		/// </summary>
		public Enum SelectedItem { get; set; }

		#endregion

		#region 方法

		private string GetCustomText(Enum enumItem)
		{
			var converter = new BindAsKeyFindValueInParaConverter();
			return converter.Convert(enumItem, typeof(string), _keyValuePairStr, CultureInfo.CurrentCulture) + "";
		}

		#endregion
	}
}
